<?php

namespace Lib;
/**
 * Class description
 * 
 * @since 1.0
 * @author Anton Patrikeyev <anton.patrikeyev@gmail.com>
 */
class Sitemap {
    /**
     * @var \ArrayObject|null
     */
    private $_data = null;

    /**
     * @var null|string
     */
    private $_rootPath = null;

    /**
     * @var null|string
     */
    private $_rootElement = null;

    /**
     * @var string
     */
    private $_output = '';

    private $_sitename = 'http://myauto.ua';

    /**
     * @param null|string $rootPath
     * @param null|string $rootElement
     */
    public function __construct($rootPath = null, $rootElement = null){
        $this->_data = new \ArrayObject();
        $this->_rootPath = (null === $rootPath)? realpath(APPLICATION_PATH . '/../public/sitemaps') : $rootPath;
        $this->_rootElement = (null === $rootElement)? 'Sitemap.xml' : $rootElement;
    }

    /**
     * @throws SitemapException
     * @param  $data
     * @return void
     */
    public function setData($data){
        if(!Data::is_array_accessible($data)) throw new SitemapException('None data given');
        $this->_data->append($data);
    }

    /**
     * @return void
     */
    public function save(){
        $convertor = new ArrayToXML('urlset', 'url', 'http://www.sitemaps.org/schemas/sitemap/0.9');
        foreach($this->_data as $dataNode){
           $convertor->createNode($dataNode);
            echo "+";
        }
        $convertor->formatOutput = true;

        // Get and compress data
        $this->_output = $convertor->saveXML();
        $gzdata = gzencode($this->_output, 9);

        // Save data
        $fp = fopen($this->_rootPath."/{$this->_rootElement}.gz", "w+");
        fwrite($fp, $gzdata);
        fclose($fp);
    }

    /**
     * @return void
     */
    public function clearData(){
        $this->_data = new \ArrayObject();
    }

    /**
     * @return void
     */
    public function updateSitemapList(){
        $dirIterator = new \FilesystemIterator($this->_rootPath);
        $currentMap = new ArrayToXML('sitemapindex', 'sitemap', 'http://www.sitemaps.org/schemas/sitemap/0.9');
        $currentMap->formatOutput = true;
        $elements = array();
        while($dirIterator->valid()){
            if($dirIterator->isFile() && $dirIterator->isReadable()){
                $fileType = array_pop(explode('.',$dirIterator->getFilename()));
                if($fileType == 'gz'){
                    $lastmod = new \DateTime();
                    $lastmod->setTimestamp($dirIterator->getMTime());
                    $elements[] = array(
                        'loc'=>$this->_sitename .'/sitemaps/'.$dirIterator->getFilename(),
                        'lastmod'=>$lastmod->format('Y-m-d').'T'.$lastmod->format('H:i:s+02:00'),
                    );
                }
            }
            $dirIterator->next();
        }
        $currentMap->createNode($elements);
        $currentMap->save(realpath(APPLICATION_PATH.'/../public').'/sitemap.xml');
    }
}

/**
 * 
 */
class SitemapException extends \Exception{}